package top.shmly.universal.utils;

import io.jsonwebtoken.Claims;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Date;
import java.util.Map;
import java.util.function.Function;

public interface JwtUtils {

    /**
     * @param userDetails
     * @return String
     * @Author: Jibny
     * @Description: 通过doGenerateToken生成token
     * @Created: 2021/4/25 11:09
     */
    String generateToken(UserDetails userDetails);

    /**
     * @param claims  声明
     * @param subject 用户账号(唯一性)
     * @return String
     * @Author: Jibny
     * @Description:
     * @Created: 2021/4/25 15:41
     */
    String doGenerateToken(Map<String, Object> claims, String subject);

    /**
     * @param createdDate
     * @return Date
     * @Author: Jibny
     * @Description: 生成token时候计算token到期时间
     * @Created: 2021/4/25 15:41
     */
    Date calculateExpirationDate(Date createdDate);

    /**
     * @param token
     * @param userDetails
     * @return Boolean
     * @Author: Jibny
     * @Description: 校验token的合法性
     * @Created: 2021/4/25 15:41
     */
    Boolean validateToken(String token, UserDetails userDetails);

    /**
     * @param token
     * @return String
     * @Author: Jibny
     * @Description: 通过getClaimFromToken，从token中获取用户Subject信息
     * @Created: 2021/4/25 15:51
     */
    String getUsernameFromToken(String token);

    <T> T getClaimFromToken(String token, Function<Claims, T> claimsResolver);

    /**
     * @param token
     * @return Claims
     * @Author: Jibny
     * @Description: 从token中获取有效载荷
     * @Created: 2021/4/25 15:59
     */
    Claims getAllClaimsFromToken(String token);

    /**
     * @param token
     * @return Boolean
     * @Author: Jibny
     * @Description: 判断token是否过期
     * @Created: 2021/4/25 15:59
     */
    Boolean isTokenExpired(String token);

    /**
     * @param token
     * @return Date
     * @Author: Jibny
     * @Description: 判断token是否过期时，从令牌获取到期日期
     * @Created: 2021/4/25 16:02
     */
    Date getExpirationDateFromToken(String token);
}
